package com.ruoyi.cderp.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.cderp.domain.SettleSheet;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Random;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.domain.ProductionWarehousingItem;
import com.ruoyi.cderp.mapper.ProductionWarehousingMapper;
import com.ruoyi.cderp.domain.ProductionWarehousing;
import com.ruoyi.cderp.service.IProductionWarehousingService;

/**
 * 产品入库Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-15
 */
@Service
public class ProductionWarehousingServiceImpl implements IProductionWarehousingService
{
    @Autowired
    private ProductionWarehousingMapper productionWarehousingMapper;

    /**
     * 查询产品入库
     *
     * @param id 产品入库主键
     * @return 产品入库
     */
    @Override
    public ProductionWarehousing selectProductionWarehousingById(String id)
    {
        ProductionWarehousing productionWarehousing = productionWarehousingMapper.selectProductionWarehousingById(id);
        productionWarehousing.setProductionWarehousingItemList(productionWarehousingMapper.selectProductionWarehousingItemById(id));
        return productionWarehousing;
    }

    /**
     * 查询产品入库列表
     *
     * @param productionWarehousing 产品入库
     * @return 产品入库
     */
    @Override
    public List<ProductionWarehousing> selectProductionWarehousingList(ProductionWarehousing productionWarehousing)
    {
        return productionWarehousingMapper.selectProductionWarehousingList(productionWarehousing);
    }

    /**
     * 新增产品入库
     *
     * @param productionWarehousing 产品入库
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProductionWarehousing(ProductionWarehousing productionWarehousing)
    {
        productionWarehousing.setCreateTime(DateUtils.getNowDate());
        // 获取当前登录用户对象
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //创建人名称
        productionWarehousing.setCreateBy(loginUser.getUser().getUserName());
        int rows = productionWarehousingMapper.insertProductionWarehousing(productionWarehousing);
        insertProductionWarehousingItem(productionWarehousing);
        return rows;
    }

    /**
     * 修改产品入库
     *
     * @param productionWarehousing 产品入库
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProductionWarehousing(ProductionWarehousing productionWarehousing)
    {
        productionWarehousing.setUpdateTime(DateUtils.getNowDate());
        // 获取当前登录用户对象
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //修改人名称
        productionWarehousing.setUpdateBy(loginUser.getUser().getUserName());
        productionWarehousingMapper.deleteProductionWarehousingItemByWarehousingId(productionWarehousing.getId());
        insertProductionWarehousingItem(productionWarehousing);
        return productionWarehousingMapper.updateProductionWarehousing(productionWarehousing);
    }

    /**
     * 批量删除产品入库
     *
     * @param ids 需要删除的产品入库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductionWarehousingByIds(String[] ids)
    {
        productionWarehousingMapper.deleteProductionWarehousingItemByWarehousingIds(ids);
        return productionWarehousingMapper.deleteProductionWarehousingByIds(ids);
    }

    /**
     * 删除产品入库信息
     *
     * @param id 产品入库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductionWarehousingById(String id)
    {
        productionWarehousingMapper.deleteProductionWarehousingItemByWarehousingId(id);
        return productionWarehousingMapper.deleteProductionWarehousingById(id);
    }

    @Override
    public List<ProductionWarehousingItem> selectOrderWarehousingList(String id) {
        return productionWarehousingMapper.selectOrderWarehousingList(id);
    }

    /**
     * 新增产品入库明细信息
     *
     * @param productionWarehousing 产品入库对象
     */
    public void insertProductionWarehousingItem(ProductionWarehousing productionWarehousing)
    {
        List<ProductionWarehousingItem> productionWarehousingItemList = productionWarehousing.getProductionWarehousingItemList();
        String id = productionWarehousing.getId();
        if (StringUtils.isNotNull(productionWarehousingItemList))
        {
            List<ProductionWarehousingItem> list = new ArrayList<ProductionWarehousingItem>();
            for (ProductionWarehousingItem productionWarehousingItem : productionWarehousingItemList)
            {
                productionWarehousingItem.setWarehousingId(id);

                //随机生成组合商品编号
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String formattedDate = sdf.format(new Date());
                Random random = new Random();
                int randomNumber = 10000000 + random.nextInt(90000000); // 生成8位数随机数
                productionWarehousingItem.setId("193" + formattedDate+randomNumber);


                list.add(productionWarehousingItem);
            }
            if (list.size() > 0)
            {
                productionWarehousingMapper.batchProductionWarehousingItem(list);
            }
        }
    }
}
